package com.xx.servlte;

import com.xx.entity.Auction;
import com.xx.entity.Auctionrecord;
import com.xx.entity.VO.AuctionRecordVo;
import com.xx.service.AuctionRecordService;
import com.xx.service.AuctionService;
import com.xx.service.Impl.AuctionRecordServiceImpl;
import com.xx.service.Impl.AuctionServiceImpl;
import org.omg.PortableInterceptor.INACTIVE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AuctionRecordServlet")
public class AuctionRecordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        switch (action) {
            case "auctionItem":
                //发布拍卖品
                auctionItem(request, response);
                break;
            case "selectBid":
                //出价记录
                selectBid(request,response);
                break;
            case "select":
                select(request,response);
                break;
        }
    }

    private void select(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        Integer auctionId=Integer.valueOf(request.getParameter("auctionId"));
        //操作
        AuctionRecordService dao=new AuctionRecordServiceImpl();
        List<AuctionRecordVo> auction=dao.getAll(auctionId);
        request.setAttribute("auction",auction);
        request.getRequestDispatcher("Bidding.jsp").forward(request,response);
    }

    private void selectBid(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //操作
        AuctionRecordService dao=new AuctionRecordServiceImpl();
        List<Auctionrecord> list=dao.getAllId();
        request.setAttribute("list",list);
        request.getRequestDispatcher("Auctions.jsp").forward(request,response);
    }

    private void auctionItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //id,userId,auctionId,auctionTime,auctionPrice
        Integer userId=Integer.valueOf(request.getParameter("userId"));
        Integer auctionId=Integer.valueOf(request.getParameter("auctionId"));
        String auctionTime=request.getParameter("auctionTime");
        String auctionPrice=request.getParameter("auctionPrice");
        Auctionrecord auctionrecord=new Auctionrecord(null,userId,auctionId,auctionTime,auctionPrice);
        AuctionRecordService dao=new AuctionRecordServiceImpl();
        dao.add(auctionrecord);
        response.sendRedirect("AuctionRecordServlet?action=selectBid");
    }
}
